package com.feature.shop.core.webmvc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import com.feature.core.constant.SecurityConstants;
import com.feature.core.security.SecurityContextHolder;
import com.feature.shop.auth.api.vo.LoginUserVo;

public class LoginUserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public boolean supportsParameter(MethodParameter parameter) {
		return parameter.getParameterType().isAssignableFrom(LoginUserVo.class);
	}

	@Override
	public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
		try {
			return SecurityContextHolder.get(SecurityConstants.LOGIN_USER_KEY);
		} catch (Exception e) {
			logger.error("", e);
		}
		return null;
	}

}
